-
Notifications
You must be signed in to change notification settings - Fork 1
10-dohyeondol1 #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
10-dohyeondol1 #37
Conversation
- dohyeondol1๋ก ์ ๋ถ ์๋ง๊ฒ ์์ ํ์์ต๋๋ค.
- ๋ฆฌ๋๋ฏธ ์์ฑํ๋๊ฑธ ์๊พธ ๊น๋ฐํ๋ค์;;
|
์ ๋ ํ์ด์ฌ์ itertools๊ฐ ๋จผ์ ๋ ์ฌ๋ผ์, ์ด๋ฅผ ํ์ฉํด์ ๋ฌธ์ ๋ฅผ ํ์์ต๋๋ค. Python3
from itertools import permutations
def power(x, y, p):
res = 1
x = x % p
while y > 0:
if y & 1:
res = (res * x) % p
y = y >> 1
x = (x*x) % p
return res
def miller_rabin(n, a):
r = 0
d = n-1
while d % 2 == 0:
r += 1
d = d//2
x = power(a, d, n)
if x == 1 or x == n-1:
return True
for i in range(r-1):
x = power(x, 2, n)
if x == n - 1:
return True
return False
def is_prime(n):
prime_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
if n == 1:
return False
if n == 2 or n == 3:
return True
if n % 2 == 0:
return False
for a in prime_list:
if n == a:
return True
if not miller_rabin(n, a):
return False
return True
def solution(numbers):
num_set = set()
for i in range(1, len(numbers)+1):
for p in permutations(numbers, i):
num = int(''.join(p))
num_set.add(num)
prime_count = 0
for num in num_set:
if is_prime(num):
prime_count += 1
return prime_count |
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
next_permutation์ ์ฌ์ฉํ๋๊น ํ์คํ ์ฝ๋๊ฐ ๊น๋ํด์ง๋ค์ ํ...
์ ์ฌ๊ท๋ก ์กฐํฉ/์์ด์ ๊ตฌํํ๋๊ฒ ํธํ๋ค ๋ณด๋ ์ง์ ์ฝ๋๋ฅผ ์ง๊ฒ ๋๋ค์.
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
bool IsPrime(const string& CandidateNumberStr)
{
int CandidateNumber = stoi(CandidateNumberStr);
if(CandidateNumber < 2)
{
return false;
}
for(int Divisor = 2; Divisor * Divisor <= CandidateNumber; ++Divisor)
{
if(CandidateNumber % Divisor == 0)
{
return false;
}
}
return true;
}
void Backtracking(const string& Numbers, vector<bool>& IsNumUsed, unordered_set<int>& PrimesSet, string CandidateNumber = "")
{
if(!CandidateNumber.empty() && IsPrime(CandidateNumber))
{
PrimesSet.emplace(stoi(CandidateNumber));
}
for(int i = 0; i < Numbers.size(); ++i)
{
if(IsNumUsed[i]) continue;
IsNumUsed[i] = true;
Backtracking(Numbers, IsNumUsed, PrimesSet, CandidateNumber + Numbers[i]);
IsNumUsed[i] = false;
}
}
int solution(string InNumbers)
{
vector<bool> IsNumUsed(InNumbers.size(), false);
unordered_set<int> PrimesSet;
Backtracking(InNumbers, IsNumUsed, PrimesSet);
return PrimesSet.size();
}
hadongun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numbers์์ ๊ฐ ์ซ์๋ก ์กฐํฉํ ์ ์๋ ๋ชจ๋ ์๋ฅผ ๋ง๋ ํ์ ๊ฐ ์๋ค์ ์์ ํ๋ณํ๋ ค ํ๋๋ฐ, ์กฐํฉํ ์ ์๋ ๋ชจ๋ ์๋ฅผ ์ด๋ป๊ฒ ๋ง๋ค์ด์ผ ํ ์ง.. ์๊ฐ์ด ์ ๋๋๊ตฐ์!
์ฐพ์ ๋ณด๋ ํ์ด์ฌ์๋ permutations์ด๋ผ๋ ํจ์๊ฐ ์์ด์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ์ต๋๋ค.!
๋ ์์ ํ๋ณ ํ ๋ ์ ๋ ์ด๋ค ์ n์ 2~n-1๊น์ง ๋๋ ์ ๋๋จธ์ง๊ฐ 0์ด ๋์ง ์๋๋ค๋ฉด ๊ทธ ์๋ ์์๋ค! ๋ผ๋ ์กฐ๊ฑด์ผ๋ก ํ๋ณํ๋๋ฐ, ๊ผญ n-1๊น์ง ๋๋ ํ์ ์์ด ๋ฃจํธn ๊น์ง๋ง ๋๋ ๋ ๋ ๋ค๋๊ฑธ ์๊ฒ๋์๋ค์ฅ
ํ์ด์ฉ
from itertools import permutations
def solution(numbers):
result = 0
l_ist = []
temp = True
for i in range(1, len(numbers) + 1):
for p in permutations(numbers, i):
l_ist.append(int(''.join(p)))
l_ist = list(set(l_ist))
for k in range(2, int(l_ist[k] ** 0.5) + 1):
if l_ist[k] < 2:
continue
nososu = False
for j in range(2, l_ist[k]):
if(l_ist[k] %j == 0):
nososu = True
if not nososu:
result += 1
return resultThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ ๋ฌธ์ ๋ฅผ ๋๋ฌด ์ค๋๋ง์ ๋ณด๋๊น ์์ํ๋ณ์ ์ด๋ป๊ฒ ํ๋์ง๋ ๊ธฐ์ต์ด ์๋ฌ๋ค์ฌ...
int limit = (int)sqrt(n);
์์๋ฅผ ํ๋ณํ ๋ ๋ฃจํธ n๊น์ง ํ๋๊ฒ๋ ์ค๋๋ง์ ๋ดค์ด๋ค. ์ด๊ฑฐ ์์๋ ๋ฌธ์ ๋ฅผ ๋ชปํ๊ฒ ์ด์ ์ฑ์ฐํผํฐ ์ฐธ๊ณ ํ์ต๋๋ค... DFS๋ก ํธ๋ ๋ฐฉ๋ฒ๋ ์๋ค์! ์๊ฐ ๋ง์์ง๋ฉด ์ด๋ป๊ฒ ๋ค ํ๋ณํด์ผ ํ ๊น๊ฐ ๊ฐ์ฅ ๋งํ๋ ๋ถ๋ถ์ด ์๋๋ฐ int next = current * 10 + (numbers[i] - '0'); ์ด๋ฐ์์ผ๋ก ๋ชจ๋ ํ๋ณํ๋ ค ํ๊ฒ ์ธ์๊น์ ๋ถ๋ถ์ธ ๊ฑฐ ๊ฐ์์! ์์๋ ๋ชปํ์๋ค์. ์ค๋ณต๋๋ ๋ถ๋ถ์ vistited๋ก ์ฒ๋ฆฌํ ๊ฒ๋ ์ ํ์ ์ธ dfs๋ฌธ์ ๊ฐ๋ค์
์จ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#define MAX_NUM 10000000 // 7์๋ฆฌ ์ซ์ ์ต๋: 9,999,999
char numbers[8]; // ์
๋ ฅ๋ ์ซ์ ๋ฌธ์์ด (์ต๋ 7์๋ฆฌ + '\0')
int len; // ๋ฌธ์์ด ๊ธธ์ด
bool used[7]; // ๊ฐ ์๋ฆฌ ์ฌ์ฉ ์ฌ๋ถ ํ์
bool visited[MAX_NUM]; // ์ด๋ฏธ ๊ฒ์ฌํ ์ซ์ ์ค๋ณต ๋ฐฉ์ง
int prime_count = 0; // ๋ฐ๊ฒฌํ ์์ ๊ฐ์
// n์ด ์์์ธ์ง ํ๋ณ
bool is_prime(int n) {
if (n < 2) return false;
int limit = (int)sqrt(n);
for (int i = 2; i <= limit; i++) {
if (n % i == 0) return false;
}
return true;
}
// ๊น์ด ์ฐ์ ํ์์ผ๋ก ๊ฐ๋ฅํ ๋ชจ๋ ์ซ์ ์กฐํฉ ์์ฑ
// current: ํ์ฌ๊น์ง ๋ง๋ค์ด์ง ์
void dfs(int current) {
for (int i = 0; i < len; i++) {
if (used[i]) continue;
used[i] = true;
int next = current * 10 + (numbers[i] - '0');
if (!visited[next]) {
visited[next] = true;
if (is_prime(next)) {
prime_count++;
}
// ๋ ๊ธด ์ซ์ ์กฐํฉ์ ์ํด ์ฌ๊ท ํธ์ถ
dfs(next);
}
used[i] = false;
}
}
int main(void) {
// ์
๋ ฅ: ์ซ์๊ฐ ์ ํ ์กฐ๊ฐ๋ค (์: "011")
if (scanf("%7s", numbers) != 1) return 0;
len = strlen(numbers);
// ์ด๊ธฐํ
memset(used, 0, sizeof(used));
memset(visited, 0, sizeof(visited));
prime_count = 0;
// ๋ชจ๋ ๊ธธ์ด(1~len)์ ์กฐํฉ ์์ฑ ์์
dfs(0);
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
printf("%d\n", prime_count);
return 0;
}
๐ ๋ฌธ์ ๋งํฌ
์ด๋ฒ ๋ฌธ์ ์ญ์ ์ฌ๋ฌ๋ถ๋ค์ ํ์ด๊ฐ ๊ถ๊ธํฉ๋๋ค.
์ ๋ฒ ๋ฌธ์ ๋ณด๋จ ์๊ฐ์ด ์กฐ๊ธ ๋ ๊ฑธ๋ฆฌ๊ฒ ์ง๋ง..! ํ์ด๋ฅผ ๊ณต์ ํด์ฃผ์ธ์
โ๏ธ ์์๋ ์๊ฐ
30๋ถ
โจ ์๋ ์ฝ๋
์ฃผ์ด์ง ๋ฌธ์์ด์์ ๊ฐ๋ฅํ ๋ชจ๋ ์์ด์ ์ฐพ์์ ์์์ธ์ง ํ๋ณํ๋ ๊ฐ๋จํ ๋ฌธ์ ์ ๋๋ค.
์ต๊ทผ์ ์ด์์ด์ ๋ํด์ ๊ณต๋ถํ๋ค๊ฐ ๋ฐฐ์ ๋ ์์ด ๊ด๋ จ ํจ์์ธ
next_permutation์ ์ฌ์ฉํด ํด๊ฒฐํ์์ต๋๋ค.next_permutation์ ํ์ฌ ๋์ ์๋ ์์ด์์์ธ์๋ก ๋์ด๊ฐ ๋ฒ์์ ํด๋นํ๋ ๋ค์ ์์ด์ ๊ตฌํ๊ณ true๋ฅผ ๋ฐํํ๋ ํจ์์ ๋๋ค.
๋ง์ฝ ๋ค์ ์์ด์ด ์๋ค๋ฉด(๋ค์์ ๋์จ ์์ด์ด ์์์ ์ด์ ์์ด๋ณด๋ค ์๋ค๋ฉด) false๋ฅผ ๋ฐํํฉ๋๋ค.
next_permutation(numbers.begin(), numbers.end())์์ ๊ฐ์ด ์ฌ์ฉํ๋ค๋ฉด numbers[0] ์ ๋ค์ ์์ด์ ๊ตฌํ๊ณ true๋ฅผ ๋ฐํํฉ๋๋ค.
์ด ์ ์ ํ์ฉํด ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฅผ ํตํด ๋ชจ๋ ์ข ์ด ์กฐ๊ฐ์ ๊ตฌํ ์ ์์ต๋๋ค.
์ด์ ์์๋ฅผ ํ๋ณํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
์์๋ 1๋ณด๋ค ํฐ ์์ฐ์ ์ค ์ฝ์๊ฐ 1๊ณผ ์๊ธฐ ์์ ๋ฟ์ธ ์น๊ตฌ์ด๊ธฐ ๋๋ฌธ์,
2๋ถํฐ ์ฃผ์ด์ง ์๋ฅผ ๋๋์ด๋ณด๋ฉด์ ํด๋น ์๊ฐ ๋๋์ด ๋จ์ด์ง๋ ์ง ํ์ธํ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ผ ์ด๋๊น์ง ๋ฐ๋ณต๋ฌธ์ ๋๋ ค์ผ ํ๋๋? ๊ทธ ๋ฒ์๊ฐ ์ ๋งคํ๋ฐ,
์ฃผ์ด์ง ์์ ์ฝ์๋ ํญ์ ์ง์ ์ด๋ฃฌ๋ค๋ ์ ์ ์ด์ฉํด์,
์ฃผ์ด์ง ์์ ์ ๊ณฑ๊ทผ('์ ๊ณฑ๊ทผ ร ์ ๊ณฑ๊ทผ = ํด๋น ์' ์ด๊ธฐ ๋๋ฌธ์ ์ฝ์์ ์ค๊ฐ์ ์์นํฉ๋๋ค.)๊น์ง๋ง ์ฌ์ฉํ์ฌ
์ฃผ์ด์ง ์๋ฅผ ์ฝ์์ ๋๋๋ฉด ๋ฉ๋๋ค.
์ฌ์ค ์๋ผํ ์คํ ๋ค์ค์ ์ฒด๋ฅผ ๊ตฌํํด์ ํ์ด๋ณด๊ณ ์ถ์๋๋ฐ,
์ด ๋ฌธ์ ์์๊น์ง ์ธ ํ์๊ฐ ์์ ๊ฒ ๊ฐ์ ๋์ค์ ์ข ๋ ๋ฐฐ์์ ์จ๋จน์ ์๊ฐ์ ๋๋ค.
์๋ ์ฝ๋
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ